package com.tjp.eight.ui.fragment.homepage;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tjp.eight.R;
import com.tjp.eight.utils.ActivityUtils;
import com.tjp.eight.utils.AppUtils;
import com.tjp.eight.utils.BitmapUtils;
import com.tjp.eight.utils.StringUtils;
import com.tjp.eight.views.emoj.EmojiUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Created by Administrator on 2017/2/18.
 */
public class HomePageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private String fromName = "青春";
    private String toName = "Mr.周";

    private ArrayList<HomeRModel.HomeRDModel> homeRDModels;

    public HomePageAdapter(Context context) {
        this.context = context;
        homeRDModels = new ArrayList<>();
    }

    private boolean isCanClick = true;

    public void setCanClick(boolean canClick) {
        isCanClick = canClick;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new HomePageViewHolder(LayoutInflater.from(context).inflate(R.layout.item_my_page_action, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((HomePageViewHolder) holder).tvComment.setText(homeRDModels.get(position).detail);
        ((HomePageViewHolder) holder).tvComment.setVisibility(StringUtils.isNullOrEmpty(homeRDModels.get(position).detail) ? View.GONE : View.VISIBLE);
        ((HomePageViewHolder) holder).tvPassTime.setText(homeRDModels.get(position).pass_time);
        ((HomePageViewHolder) holder).tvTitle.setText(homeRDModels.get(position).title);
        if (isCanClick) {
            final String cid = homeRDModels.get(position).contentid + "";
            ((HomePageViewHolder) holder).rl_detail_a.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ActivityUtils.startDetails(context, cid);
                }
            });
        }
        Glide.with(context).load(homeRDModels.get(position).cover_path).placeholder(R.drawable.loading).
                error(R.drawable.icon_fail).into(((HomePageViewHolder) holder).iv_my_p_action_pic);
        setImg(homeRDModels.get(position).icon_type, ((HomePageViewHolder) holder).iv_type);
        ArrayList<String> names = AppUtils.getNames(homeRDModels.get(position).detail);
        ArrayList<String> uids = AppUtils.getUids(homeRDModels.get(position).detail);
        String[] split = AppUtils.getSplit(homeRDModels.get(position).detail);
        if (names != null && !names.isEmpty())
            AppUtils.getDetail(new Clickable(clickListener), new Clickable(clickListener), split, names, ((HomePageViewHolder) holder).tvComment, context);
        else {
            try {
                new EmojiUtil().handlerEmojiText(((HomePageViewHolder) holder).tvComment, homeRDModels.get(position).detail, context);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    void setImg(int type, ImageView view) {
        switch (type) {
            case 1://icon_type {"1":"阅读","2":"评论","3":"分享","4":"赞别人的评论","5":"赞别人的回复","6":"回复"}
                view.setImageBitmap(BitmapUtils.getBitmap(context, R.drawable.act_m_read));
                break;
            case 2:
                view.setImageBitmap(BitmapUtils.getBitmap(context, R.drawable.act_m_comment));
                break;
            case 3:
                view.setImageBitmap(BitmapUtils.getBitmap(context, R.drawable.act_m_share));
                break;
            case 4:
                //view.setImageBitmap(BitmapUtils.getBitmap(context, R.drawable.act_m_zan_o));
                view.setImageBitmap(BitmapUtils.getBitmap(context, R.drawable.act_collect));
                break;
            case 5:
                view.setImageBitmap(BitmapUtils.getBitmap(context, R.drawable.act_m_zan_o_r));
                break;
            case 6:
                view.setImageBitmap(BitmapUtils.getBitmap(context, R.drawable.act_m_reply));
                break;
        }
    }

    public void removeItem(int position) {
        if (position < 0 || position >= homeRDModels.size()) return;
        if (unCollection != null) {
            String contentid = homeRDModels.get(position).contentid + "";
            String type = homeRDModels.get(position).contentid_type + "";
            unCollection.unCollection(contentid,type,position);
        }

    }

    public void reMoveItemSuccess(int position) {
        if (position < 0 || position >= homeRDModels.size()) return;
        homeRDModels.remove(position);
        notifyDataSetChanged();
    }

    public void startDetails(int position) {
        ActivityUtils.startDetails(context, homeRDModels.get(position).contentid + "");
    }

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        }
    };

    @Override
    public int getItemCount() {
        return homeRDModels.size();
    }

    public void addData(HomeRModel.HomeRDModel homeRDModel) {
        if (homeRDModel == null) return;
        int index = homeRDModels.size();
        homeRDModels.add(homeRDModel);
        notifyItemRangeInserted(index, getItemCount());
    }

    public void addData(ArrayList<HomeRModel.HomeRDModel> homeRDModel) {
        if (homeRDModel == null || homeRDModel.isEmpty()) return;
        int index = homeRDModels.size();
        for (HomeRModel.HomeRDModel model : homeRDModel)
            homeRDModels.add(model);
        notifyItemRangeInserted(index, getItemCount());
    }

    interface UnCollection {
        void unCollection(String contentId,String type,int position);
    }

    UnCollection unCollection;

    public void setUnCollection(UnCollection unCollection) {
        this.unCollection = unCollection;
    }

    class Clickable extends ClickableSpan {
        private final View.OnClickListener mListener;

        public Clickable(View.OnClickListener l) {
            mListener = l;
        }

        /**
         * 重写父类点击事件
         */
        @Override
        public void onClick(View v) {
            mListener.onClick(v);
        }

        /**
         * 重写父类updateDrawState方法  我们可以给TextView设置字体颜色,背景颜色等等...
         */
        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(Color.parseColor("#0BBBF8"));
        }
    }
}
